package de.koller.nature.proto;

import de.koller.oceanFramework.gl.util.ImagePreview;
import de.koller.oceanFramework.util.ColorIntUtil;
import de.koller.so.render.logo.LogoRenderer;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;

public class Terrain2D {
	
	public static void main( String[] args ) {
//		BufferedImage bi = new BufferedImage( 1280, 100, BufferedImage.TYPE_INT_ARGB );
//		for( int x = 0; x < 1280; x++ )
//			for( int y = 0; y < 100; y++ )
//				bi.setRGB( x, y, getColor( x ) );
//		
//		ImagePreview.showImage( bi, 1000, 100 );
//		
		if( args.length != 0 )
			terrain( Integer.parseInt( args[0] ) );
		else
			terrain( 100 );
	}

	public static int getColor( int v ) {
		if( v < 256 )
			return ColorIntUtil.getIntColor( 0xff, 0, 0, v );
		if( v < 512 )
			return ColorIntUtil.getIntColor( 0xff, 0, v, 0xff-(v-256) );
		if( v < 768 )
			return ColorIntUtil.getIntColor( 0xff, v-512, 0xff, 0 );
		if( v < 1024 )
			return ColorIntUtil.getIntColor( 0xff, 0xff, 0xff-(v-768), 0 );
		if( v < 1280 )
			return ColorIntUtil.getIntColor( 0xff, 0xff, (v-1024), (v-1024) );
		return 0xffffffff;
	}
	
	public static int done = 0;
	public static int[][] terrain( int si ) {
		Random rnd = new Random();
		final int fw = si, fh = si;
		final int checkSize = 64;
		
		BufferedImage img = new BufferedImage( fw, fh, BufferedImage.TYPE_INT_ARGB );
		final int[][] tp = new int[ fw + 2*checkSize ][ fh + 2*checkSize ];
		final int[][] th = new int[ fw ][ fh ];
		
		for( int x = 0; x < tp.length; x++ )
			for( int y = 0; y < tp[0].length; y++ ) {
				int t = rnd.nextInt( 2 );
				tp[x][y] = t;
				
//				if( x < checkSize /*|| y < checkSize || x > tp.length-checkSize || y > tp[0].length-checkSize */ )
//					tp[x][y] = 0;
			}
		
		ExecutorService s = Executors.newFixedThreadPool( 4 );
		
		for( int x = 0; x < fw; x++ ) {
			final int xv = x;
			s.execute( new Runnable() {
				@Override public void run() {
					for( int y = 0; y < fh; y++ ) {
						for( int dx = -checkSize; dx < checkSize; dx++ )
							for( int dy = -checkSize; dy < checkSize; dy++ ) {
								double dis = Math.sqrt( dx*dx + dy*dy );
								if( dis < checkSize )
									th[xv][y] += tp[ xv + checkSize + dx ][ y + checkSize + dy ];
							}
					}
					synchronized( this ) {
						done++;
					}
					System.out.println( xv + " : " + done );
				}
			} );
		}
		
		try {
			s.shutdown();
			s.awaitTermination( 1, TimeUnit.DAYS );
		} catch ( InterruptedException ex ) {
			
		}
		
		int max = 0;
		int min = Integer.MAX_VALUE;
		for( int x = 0; x < fw; x++ ) {
			for( int y = 0; y < fh; y++ ) {
				max = Math.max( th[x][y], max );
				min = Math.min( th[x][y], min );
			}
			System.out.println( "Maxmin" + x );
		}
		
		double c = 1280. / (max - min);
		for( int x = 0; x < th.length; x++ ) {
			for( int y = 0; y < th[0].length; y++ ) {
				int k = (int)((th[x][y] - min) * c);
				img.setRGB( x, y, getColor( (int)k ) );
			}
			System.out.println( "pic" + x );
		}
		
		try {
			ImageIO.write( img, "png", new File( "./terrain.png" ) );
		} catch( IOException ex ) { System.err.println( ex ); }
		//ImagePreview.showImage( img, img.getWidth()*7, img.getHeight()*7 );
		return th;
	}
	
}
